package com.timeDeal.servlet;




import com.alibaba.druid.util.StringUtils;
import com.timeDeal.bean.UserBean;
import com.timeDeal.bean.XuqiufabuBean;
import com.timeDeal.service.XuqiuService;


import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.io.PrintWriter;
import java.sql.Date;
import java.text.SimpleDateFormat;


/**
 * Created by Administrator on 2016/7/24.
 */

public class XuqiuFabuServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD HH:MM:SS");
        UserBean loginuser = (UserBean) request.getSession().getAttribute("USER");
        String xuqiumingcheng=request.getParameter("xuqiumingcheng");
        String xuqiugaishu=request.getParameter("xuqiugaishu");
        String choulaojine=request.getParameter("choulaojine");
        String wanchengqixian=request.getParameter("wanchengqixian");
        String xiangxiyaoqiu=request.getParameter("xiangxiyaoqiu");
        String shifoujingbiao=request.getParameter("shifoujingbiao");
        String choulaoshangxian=request.getParameter("choulaoshangxian");
        String jingbiaoqixian=request.getParameter("jingbiaoqixian");
        XuqiufabuBean xfb = new XuqiufabuBean();
        Date adate =  new Date(new java.util.Date().getTime()) ;
        jingbiaoqixian= StringUtils.isEmpty(jingbiaoqixian)?"1995-05-16":jingbiaoqixian;
        xfb.setWanchengshijian(Date.valueOf(wanchengqixian));
        xfb.setJiafangid(loginuser.getYonghuid());
        xfb.setXuqiumingcheng(xuqiumingcheng);
        xfb.setFabushijian(adate);
        xfb.setXuqiuyaoqiu(xiangxiyaoqiu);
        xfb.setChoulao(Integer.valueOf(choulaojine));
        if(StringUtils.equals(shifoujingbiao,"是")){
            xfb.setChoulaoshangxian(Integer.valueOf(choulaoshangxian));
            xfb.setJingbiaoriqi(Date.valueOf(jingbiaoqixian));
        }
        else{
            xfb.setChoulaoshangxian(0);
            xfb.setJingbiaoriqi(null);
        }
        xfb.setYunxujingbiao(shifoujingbiao);
        xfb.setMiaoshu(xuqiugaishu);
        XuqiuService xs = new XuqiuService();
        if(xs.addAXuqiu(xfb)) {
            PrintWriter out = response.getWriter();
            out.print("<script contentType=\"text/html;charset=UTF-8\" language=\"java\" >alert('fabu sucessful'); </script>");
            request.getRequestDispatcher("/user/zhujiemiangg.jsp?leixing=xuqiufabu&tiaojiao=redu&currentpage=1").forward(request,response);
        }
        else {
            PrintWriter out = response.getWriter();
            out.print("<script contentType=\"text/html;charset=UTF-8\" language=\"java\" >alert('fabu fail'); </script>");
            response.setHeader("refresh", "0;url=/TimeDeal/user/zhujiemian.jsp");
        }
    }
}
